package com.sise.vo;


import com.sise.constants.WsCacheConstants;
import com.sise.constants.WsResultConstants;

import java.io.Serializable;

/**
 * web service 公共参数返回VO
 *
 * @author idea
 * @date 2016/6/2
 */
public class WsResponseVO implements Serializable {

    private static final long serialVersionUID = 5353683574922132L;

    /**
     * 请求是否成功
     */
    private boolean succeed;

    /**
     * 状态码
     */
    private String code;

    /**
     * 状态消息
     */
    private String msg;

    /**
     * 缓存是否命中
     */
    private String cache = WsCacheConstants.MISS;

    public boolean isSucceed() {
        return succeed;
    }

    public void setSucceed(boolean succeed) {
        this.succeed = succeed;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCache() {
        return cache;
    }

    public void setCache(String cache) {
        this.cache = cache;
    }

    public void setCacheWithBoolean(boolean isCacheHit) {
        cache = isCacheHit ? WsCacheConstants.HIT : cache;
    }

    /**
     * 构造没有body节点的成功响应数据.
     *
     * @return
     */
    public static WsResponseVO builderSuccessRespData() {
        WsResponseVO response = new WsResponseVO();
        response.setSucceed(true);
        response.setCode(WsResultConstants.WS_CODE_SUCCESS);
        response.setMsg(WsResultConstants.WS_MSG_SUCCESS);
        return response;
    }

    /**
     * 构造参数错误响应数据.
     */
    public static WsResponseVO builderFailedErrParam() {
        WsResponseVO response = new WsResponseVO();
        response.setSucceed(false);
        response.setCode(WsResultConstants.WS_CODE_ERR_PARAM);
        response.setMsg(WsResultConstants.WS_MSG_ERR_PARAM);
        return response;
    }


    /**
     * 构造参数错误响应数据.
     */
    public static WsResponseMsgVO builderFailedErrParam(Object data) {
        WsResponseMsgVO response = new WsResponseMsgVO();
        response.setSucceed(false);
        response.setData(data);
        response.setCode(WsResultConstants.WS_CODE_ERR_PARAM);
        response.setMsg(WsResultConstants.WS_MSG_ERR_PARAM);
        return response;
    }

    /**
     * 构造服务器未知异常响应数据.
     */
    public static WsResponseVO builderFailedRespData() {
        WsResponseVO response = new WsResponseVO();
        response.setSucceed(false);
        response.setCode(WsResultConstants.WS_CODE_ERR_UNKNOW);
        response.setMsg(WsResultConstants.WS_MSG_ERR_UNKNOW);
        return response;
    }

    /**
     * 构造异常响应数据.
     */
    public static WsResponseVO builderFailedRespData(String msg) {
        WsResponseVO response = new WsResponseVO();
        response.setSucceed(false);
        response.setCode(WsResultConstants.WS_CODE_ERR_UNKNOW);
        response.setMsg(msg);
        return response;
    }

    /**
     * 构造异常响应数据.
     */
    public static WsResponseVO builderFailedRespData(String code, String msg) {
        WsResponseVO response = new WsResponseVO();
        response.setSucceed(false);
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }

    /**
     * 构造找不到数据响应数据.
     */
    public static WsResponseVO builderRespNoDataFound() {
        WsResponseVO response = new WsResponseVO();
        response.setSucceed(false);
        response.setCode(WsResultConstants.WS_CODE_NO_DATA_FOUND);
        response.setMsg(WsResultConstants.WS_MSG_NO_DATA_FOUND);
        return response;
    }

    /**
     * 构造token错误响应数据.
     */
    public static WsResponseVO builderTokenError() {
        WsResponseVO response = new WsResponseVO();
        response.setSucceed(false);
        response.setCode(WsResultConstants.WS_TOKEN_ERROR_BIZCODE);
        response.setMsg(WsResultConstants.WS_MSG_TOKEN_ERROR_BIZCODE);
        return response;
    }

    /**
     * 构造登录异常响应数据.
     */
    public static WsResponseVO builderRespLoginFail() {
        WsResponseVO response = new WsResponseVO();
        response.setSucceed(false);
        response.setCode(WsResultConstants.WS_LOGIN_ERROR_BIZCODE);
        response.setMsg(WsResultConstants.WS_MSG_LOGIN_ERROR_BIZCODE);
        return response;
    }

    /**
     * 构造未找到业务处理逻辑
     *
     * @return
     */
    public static WsResponseVO builderHandlerNoFound() {
        return builderRespData(false, WsResultConstants.WS_CODE_ERR_BIZCODE, WsResultConstants.WS_MSG_ERR_BIZCODE);
    }

    /**
     * 构造自定义响应应数据
     *
     * @param code
     * @param msg
     */
    public static WsResponseVO builderRespData(Boolean isSucceed, String code, String msg) {
        WsResponseVO response = new WsResponseVO();
        response.setSucceed(isSucceed);
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }

}
